package com.xinyue.exceltool.logic.convert;

import com.xinyue.exceltool.logic.convert.impl.*;

import java.util.function.Supplier;

/**
 * <p>
 * 数据类型配置枚举类，并添加了数据类型转换接口
 * </p>
 *
 * @Author 王广帅
 * @Date 2021/5/1 2:34 上午
 */
public enum CellValueTypeStrategyEnum {
    INT("int", () -> new IntConvert()),
    INT_ARRAY("int[]", () -> new IntArrayConvert()),
    LONG("long", () -> new LongConvert()),
    LONG_ARRAY("long[]", () -> new LongArrayConvert()),
    STRING("string", () -> new StringConvert()),
    STRING_ARRAY("string[]", () -> new StringArrayConvert()),
    JSON_OBJ("json_obj", () -> new JsonObjConvert()),
    JSON_ARRAY("json_array", () -> new JsonArrayConvert()),
    FLOAT("float", () -> new FloatConvert()),
    FLOAT_ARRAY("float[]", () -> new FloatArrayConvert()),
    BOOLEAN("bool", () -> new BooleanConvert()),
    ;
    private String type;
    private Supplier<ICellValueTypeStrategy> cellValueConvert;

    CellValueTypeStrategyEnum(String type, Supplier<ICellValueTypeStrategy> cellValueConvert) {
        this.type = type;
        this.cellValueConvert = cellValueConvert;
    }

    public String getType() {
        return type;
    }

    public Supplier<ICellValueTypeStrategy> getCellValueConvert() {
        return cellValueConvert;
    }


    public static ICellValueTypeStrategy getValueConvert(String type) {
        for (CellValueTypeStrategyEnum convertTypeEnum : CellValueTypeStrategyEnum.values()) {
            if (convertTypeEnum.getType().equals(type)) {
                return convertTypeEnum.getCellValueConvert().get();
            }
        }
        throw new IllegalArgumentException("不存在的数据类型:" + type);
    }
}
